/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.forms.tdkhoa;

import com.aptech.dao.UserManager;
import com.aptech.validate.AccountValidator;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author SONY
 */
public class ChangePassword extends javax.swing.JFrame {

    private Properties p2;

    /**
     * Creates new form ChangePassword
     */
    public ChangePassword() {
        initComponents();
        setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jlblAccList_Username = new javax.swing.JLabel();
        jlblAccList_Password = new javax.swing.JLabel();
        jlblAccList_Password2 = new javax.swing.JLabel();
        jpwChangePassword_CurrentPassword = new javax.swing.JPasswordField();
        jpwChangePassword_ConfirmNewPassword = new javax.swing.JPasswordField();
        jpwChangePassword_NewPassword = new javax.swing.JPasswordField();
        jlblAccList_Username1 = new javax.swing.JLabel();
        jtfChangePassword_UserName = new javax.swing.JTextField();
        jbtChangePassword_Submit = new javax.swing.JButton();
        jbtChangePassoword_Reset = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Change Password");
        setIconImage(new ImageIcon(getClass().getResource("/com/image/icon.png")).getImage());
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Segoe UI Light", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 0, 0));
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/054.png"))); // NOI18N
        jLabel1.setText("Change Password");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Information", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Segoe UI Light", 1, 14), new java.awt.Color(255, 0, 0))); // NOI18N

        jlblAccList_Username.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jlblAccList_Username.setForeground(new java.awt.Color(51, 0, 255));
        jlblAccList_Username.setText("Your password");

        jlblAccList_Password.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jlblAccList_Password.setForeground(new java.awt.Color(51, 0, 255));
        jlblAccList_Password.setText("New password");

        jlblAccList_Password2.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jlblAccList_Password2.setForeground(new java.awt.Color(51, 0, 255));
        jlblAccList_Password2.setText("Confirm new password");
        jlblAccList_Password2.setToolTipText("");

        jpwChangePassword_CurrentPassword.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jpwChangePassword_CurrentPassword.setToolTipText("Typical your password");

        jpwChangePassword_ConfirmNewPassword.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jpwChangePassword_ConfirmNewPassword.setToolTipText("Typical your password");

        jpwChangePassword_NewPassword.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jpwChangePassword_NewPassword.setToolTipText("Typical your password");

        jlblAccList_Username1.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jlblAccList_Username1.setForeground(new java.awt.Color(51, 0, 255));
        jlblAccList_Username1.setText("User name");

        jtfChangePassword_UserName.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jlblAccList_Password)
                        .addGap(18, 18, 18)
                        .addComponent(jpwChangePassword_NewPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jlblAccList_Password2)
                        .addGap(18, 18, 18)
                        .addComponent(jpwChangePassword_ConfirmNewPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jlblAccList_Username)
                            .addComponent(jlblAccList_Username1))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jtfChangePassword_UserName)
                            .addComponent(jpwChangePassword_CurrentPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE))))
                .addContainerGap(15, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfChangePassword_UserName, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblAccList_Username1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jpwChangePassword_CurrentPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblAccList_Username))
                .addGap(34, 34, 34)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jpwChangePassword_NewPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblAccList_Password))
                .addGap(33, 33, 33)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jpwChangePassword_ConfirmNewPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblAccList_Password2))
                .addGap(21, 21, 21))
        );

        jbtChangePassword_Submit.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jbtChangePassword_Submit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/003.png"))); // NOI18N
        jbtChangePassword_Submit.setText("Submit");
        jbtChangePassword_Submit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtChangePassword_SubmitActionPerformed(evt);
            }
        });

        jbtChangePassoword_Reset.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jbtChangePassoword_Reset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/016.png"))); // NOI18N
        jbtChangePassoword_Reset.setText("Reset");
        jbtChangePassoword_Reset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtChangePassoword_ResetActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(62, 62, 62)
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE)
                .addGap(60, 60, 60))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(76, 76, 76)
                .addComponent(jbtChangePassword_Submit)
                .addGap(29, 29, 29)
                .addComponent(jbtChangePassoword_Reset, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtChangePassword_Submit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtChangePassoword_Reset, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(28, 28, 28))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtChangePassword_SubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtChangePassword_SubmitActionPerformed
        try {
            String oldPassword = String.valueOf(jpwChangePassword_CurrentPassword.getPassword());
            String newPassword = String.valueOf(jpwChangePassword_NewPassword.getPassword());
            String confirmNewPassword = String.valueOf(jpwChangePassword_ConfirmNewPassword.getPassword());
            String userName = jtfChangePassword_UserName.getText().trim();

            boolean checkNewPassword = AccountValidator.validatePassword(newPassword);
            if (checkNewPassword) {
                if (newPassword.equals(confirmNewPassword)) {
                    if (UserManager.checkUsername(jtfChangePassword_UserName.getText().trim()) != null) {
                        String oldPasswordEncrypted = AccountValidator.EncryptPassword(oldPassword);
                        String newPasswordEncrypted = AccountValidator.EncryptPassword(newPassword);
                        boolean result = UserManager.changePassword(newPasswordEncrypted, userName, oldPasswordEncrypted);
                        if (result) {
                            JOptionPane.showMessageDialog(this, "Changing password successfully!", "Message", JOptionPane.INFORMATION_MESSAGE);
                        } else {
                            JOptionPane.showMessageDialog(this, "Changing password failed!", "Message", JOptionPane.ERROR_MESSAGE);
                        }
                    } else {
                        JOptionPane.showMessageDialog(this, "Not Existing Username!", "Message", JOptionPane.ERROR_MESSAGE);
                    }
                }

            } else {
                JOptionPane.showMessageDialog(this, "Your new Password invalid format!", "Message", JOptionPane.ERROR_MESSAGE);
            }

        } catch (SQLException ex) {
            Logger.getLogger(ChangePassword.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(ChangePassword.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jbtChangePassword_SubmitActionPerformed

    

    private void jbtChangePassoword_ResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtChangePassoword_ResetActionPerformed
        clearForm();
    }//GEN-LAST:event_jbtChangePassoword_ResetActionPerformed

    private void clearForm() {
        jpwChangePassword_CurrentPassword.setText(null);
        jpwChangePassword_NewPassword.setText(null);
        jpwChangePassword_ConfirmNewPassword.setText(null);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ChangePassword.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ChangePassword.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ChangePassword.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ChangePassword.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ChangePassword().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbtChangePassoword_Reset;
    private javax.swing.JButton jbtChangePassword_Submit;
    private javax.swing.JLabel jlblAccList_Password;
    private javax.swing.JLabel jlblAccList_Password2;
    private javax.swing.JLabel jlblAccList_Username;
    private javax.swing.JLabel jlblAccList_Username1;
    private javax.swing.JPasswordField jpwChangePassword_ConfirmNewPassword;
    private javax.swing.JPasswordField jpwChangePassword_CurrentPassword;
    private javax.swing.JPasswordField jpwChangePassword_NewPassword;
    private javax.swing.JTextField jtfChangePassword_UserName;
    // End of variables declaration//GEN-END:variables
}
